package Task;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

//@author A0094762W
/**
 * Stores instances of DSTask in an ArrayList. Allows for retrieval and
 * provides a sort function to sort the tasks.
 */
public class DSTaskList {
	public static final int SORT_DEFAULT = 0;

	private ArrayList<DSTask> _tasks;

	public DSTaskList() {
		this._tasks = new ArrayList<DSTask>();
	}

	public boolean add(DSTask task) {
		return this._tasks.add(task);
	}

	public DSTask remove(int index) {
		return this._tasks.remove(index);
	}

	public DSTask set(int index, DSTask task) {
		return this._tasks.set(index, task);
	}

	public DSTask get(int index) {
		return this._tasks.get(index);
	}

	public int indexOf(DSTask task) {
		String taskId = task.getTaskId();
		for (int i = 0; i < _tasks.size(); i++) {
			if (_tasks.get(i).getTaskId().equals(taskId)) {
				return i;
			}
		}
		return -1;
	}

	public int indexOf(String taskId) {
		for (int i = 0; i < _tasks.size(); i++) {
			if (_tasks.get(i).getTaskId().equals(taskId)) {
				return i;
			}
		}
		return -1;
	}

	public Object[] getArray() {

		Object[] array = new Object[size()];

		for (int i = 0; i < size(); i++) {
			array[i] = _tasks.get(i);
		}

		return array;
	}

	public int size() {
		return this._tasks.size();
	}

	public DSTaskList copy() throws ParseException {
		DSTaskList newList = new DSTaskList();
		for (int i = 0; i < _tasks.size(); i++)
			newList.add(_tasks.get(i).copy());
		return newList;
	}

	public void sort(int sortType) {
		switch (sortType) {
		case SORT_DEFAULT:
			Collections.sort(_tasks);
			break;

		default:
			System.out.println("Error: Invalid Sort Type");
			break;
		}
	}
}